#!/bin/bash

function min_equ {
    [ ! -f $1/min01.tpr ] && { gmx grompp -c $1/system.pdb -r $1/system.pdb -f $1/min01.mdp -p $1/system.top -o $1/min01.tpr -n $1/index.ndx -po $1/min01_mdout.mdp -maxwarn 7 >$1/min01_grompp.log 2>&1 || { echo "Failed to run command  at line ${LINENO}" && exit; }; }
    [ ! -f $1/min01.gro ] && { (cd $1/ && gmx mdrun --deffnm min01 >min01_mdrun_log.log 2>&1) || { echo "Failed to run command  at line ${LINENO}" && exit; }; }
    [ ! -f $1/min02.tpr ] && { gmx grompp -c $1/min01.gro -r $1/min01.gro -f $1/min02.mdp -p $1/system.top -o $1/min02.tpr -n $1/index.ndx -po $1/min02_mdout.mdp -maxwarn 7 >$1/min02_grompp.log 2>&1 || { echo "Failed to run command  at line ${LINENO}" && exit; }; }
    [ ! -f $1/min02.gro ] && { (cd $1/ && gmx mdrun --deffnm min02 >min02_mdrun_log.log 2>&1) || { echo "Failed to run command  at line ${LINENO}" && exit; }; }
    [ ! -f $1/min03.tpr ] && { gmx grompp -c $1/min02.gro -r $1/min02.gro -f $1/min03.mdp -p $1/system.top -o $1/min03.tpr -n $1/index.ndx -po $1/min03_mdout.mdp -maxwarn 7 >$1/min03_grompp.log 2>&1 || { echo "Failed to run command  at line ${LINENO}" && exit; }; }
    [ ! -f $1/min03.gro ] && { (cd $1/ && gmx mdrun --deffnm min03 >min03_mdrun_log.log 2>&1) || { echo "Failed to run command  at line ${LINENO}" && exit; }; }
    [ ! -f $1/nve.tpr ] && { gmx grompp -c $1/min03.gro -r $1/min03.gro -f $1/nve.mdp -p $1/system.top -o $1/nve.tpr -n $1/index.ndx -po $1/nve_mdout.mdp -maxwarn 7 >$1/nve_grompp.log 2>&1 || { echo "Failed to run command  at line ${LINENO}" && exit; }; }
    [ ! -f $1/nve.gro ] && { (cd $1/ && gmx mdrun --deffnm nve >nve_mdrun_log.log 2>&1) || { echo "Failed to run command  at line ${LINENO}" && exit; }; }
    [ ! -f $1/nvt.tpr ] && { gmx grompp -c $1/nve.gro -r $1/nve.gro -f $1/nvt.mdp -p $1/system.top -o $1/nvt.tpr -n $1/index.ndx -po $1/nvt_mdout.mdp -maxwarn 7 >$1/nvt_grompp.log 2>&1 || { echo "Failed to run command  at line ${LINENO}" && exit; }; }
    [ ! -f $1/nvt.gro ] && { (cd $1/ && gmx mdrun --deffnm nvt >nvt_mdrun_log.log 2>&1) || { echo "Failed to run command  at line ${LINENO}" && exit; }; }
    [ ! -f $1/npt.tpr ] && { gmx grompp -c $1/nvt.gro -r $1/nvt.gro -f $1/npt.mdp -p $1/system.top -o $1/npt.tpr -n $1/index.ndx -po $1/npt_mdout.mdp -maxwarn 7 >$1/npt_grompp.log 2>&1 || { echo "Failed to run command  at line ${LINENO}" && exit; }; }
    [ ! -f $1/npt.gro ] && { (cd $1/ && gmx mdrun --deffnm npt >npt_mdrun_log.log 2>&1) || { echo "Failed to run command  at line ${LINENO}" && exit; }; }
    # [ ! -f $1/md_nocont.tpr ] && { gmx grompp -c $1/npt.gro -r $1/npt.gro -t $1/npt.cpt -f $1/md_nocont.mdp -po $1/mdout_nocont.mdp -o $1/md_nocont.tpr -n $1/index.ndx -p $1/system.top -maxwarn 7 > $1/mdout_nocont.log 2>&1 || { echo "Failed to run command  at line ${LINENO}" && exit; } }
    [ ! -f $1/md.tpr ] && { gmx grompp -c $1/npt.gro -r $1/npt.gro -t $1/npt.cpt -f $1/md.mdp -p $1/system.top -o $1/md.tpr -n $1/index.ndx -po $1/mdout.mdp -maxwarn 7 >$1/mdout.log 2>&1 || { echo "Failed to run command  at line ${LINENO}" && exit; }; }
}

source ~/.bashrc
#source /work/home/aixplorerbio_wz/miniconda3/bin/activate amber
source /work/home/aixplorerbio_wz/software/gromacs-2020.3/bin/GMXRC

#module switch compiler/dtk/21.10
#module load apps/Gromacs-DCU2/2020.3/hpcx-gcc-7.3.1

min_equ $1
